<?php
namespace App\Http\Controllers\Member;

use App\Model\Member;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;


class IndexController extends CommonController
{
    public function __construct()
    {
        parent::__construct();

    }

    // 登陆页面
    public function index(){
        $user = Member::checkLoginStatus();
        if(!$user){
//            return 1111;
//            return view('member/index/login');
            return redirect()->route('member.login');
        }

        return view('member/index/index')->with('seo',[
            'seo_title' => '万有云 服务',
            'seo_keywords' => '万有云 服务',
            'seo_description' => '万有云 服务',
        ]);
    }
    public function logout(Request $request){
        Member::logout();
//        $request->session()->flush();

        return redirect()->route('member.index');
//        return $logout;
    }
    //登陆处理页面
    public function checkLogin(Request $request){
        $validator = Validator::make($request->all(),[
            'phone' => 'bail|required|mobile',
            'password' => 'bail|required|min:8',
        ],[
            'phone.mobile' => '请输入11位有效手机号码',
        ]);

        if ($validator->fails()) {
            $errors = json_decode($validator->messages(),true);
            reset($errors);
            return [
                'code' => '-1',
                'msg' => $validator->errors()->first(),
                'field' => key($errors),
            ];
        }
        $reset = Member::checkLogin($request->input('phone'),$request->input('password'));
        if($reset['code'] == 1){
            $reset['url'] = route('member.index');
        }
        return $reset;
    }
    // 注册处理页面
    public function checkRegister(Request $request){
        $validator = Validator::make($request->all(),[
            'phone' => 'bail|required|mobile',
            'smscode' => 'bail|size:6',
            'password' => 'bail|required|string|min:8|max:20|confirmed',
        ],[
            'phone.mobile' => '请输入11位有效手机号码',
            'smscode.size' => '请输入6位短信验证码',
        ]);
        if ($validator->fails()) {
            $errors = json_decode($validator->messages(),true);
            reset($errors);
            return [
                'code' => '-1',
                'msg' => $validator->errors()->first(),
                'field' => key($errors),
            ];
        }
        $SendSms = new \App\Common\SendsmsController();
        $reset_check_sms = $SendSms->checkSmscode($request->input('phone'),'reg',$request->input('smscode'),$request->getClientIp());
        if($reset_check_sms['code'] != 1){
            return $reset_check_sms;
        }
        event($menber_info = Member::createDate([
                'phone' => $request->input('phone'),
                'password' => $request->input('password'),
                'ip' => $request->getClientIp(),
            ]));

        Member::saveLoginStatus($menber_info);

        return [
            'code' => '1',
            'msg' => '登陆成功',
            'url' => route('member.index'),
        ];
    }
    // 找回密码处理
    public function checkForget(Request $request){

    }
    // 获取短信验证码
    public function getSms(Request $request){

        $validator = Validator::make($request->all(),[
            'phone' => 'bail|required|mobile',
        ],[
            'phone.mobile' => '请输入11位有效手机号码',
        ]);
        if ($validator->fails()) {
            $errors = json_decode($validator->messages(),true);
            reset($errors);
            return [
                'code' => '-1',
                'msg' => $validator->errors()->first(),
                'field' => key($errors),
            ];
        }
        $SendSms = new \App\Common\SendsmsController();
        return $SendSms->sendSms($request->input('phone'),'reg','',$request->getClientIp());
    }
}
